# -*- coding: utf8 -*-
from soccerbots import strhex

class BallState:
    def __init__(self):
        self.position = (0,900)
        self.direction = 90
        self.linearVel = 0

    def toLine(self):
        return '%x,%x,%x,%x,1'%(self.position[0]+1250,self.position[1],
                                self.direction,self.linearVel)
    
    def __str__(self):
        return str(self.position)
    
    def __repr__(self):
        return self.__str__()

    @staticmethod
    def parseFromLine(line):
        state = BallState()
        parts = line.split(',')
        state.position = (strhex(parts[0])-1250,strhex(parts[1]))
        state.direction = strhex(parts[2])
        state.linearVel = strhex(parts[3])
        return state

class PlayerState:
    def __init__(self, name):
        self.position = (0,0)
        self.direction = 90
        self.linearVel = 0
        self.angVel = 0
        self.name = name

    def toLine(self):
        return '%x,%x,%x,%x,%x'%(self.position[0]+1250,self.position[1],
                                 self.direction,self.linearVel,self.angVel)
    
    def __str__(self):
        return str(self.position)
    
    def __repr__(self):
        return self.__str__()
    
    @staticmethod
    def parseFromLine(line, name):
        state = PlayerState(name)
        parts = line.split(',')
        state.position = (strhex(parts[0])-1250,strhex(parts[1]))
        state.direction = strhex(parts[2])
        state.linearVel = strhex(parts[3])
        state.angVel = strhex(parts[4])
        return state

class TeamState(dict):
    keyset = ['goalkeeper','player1','player2','player3','player4']

    def __init__(self, teamId):
        self.teamId = teamId
        for player in TeamState.keyset:
            self[player] = PlayerState(player)

    def getLines(self):
        lines = []
        for player in TeamState.keyset:
            lines.append(self[player].toLine())
        return lines

    def __str__(self):
        s = ''
        for k in TeamState.keyset:
            s += str(self[k])+' '
        return s
    
    def __repr__(self):
        return self.__str__()

    @staticmethod
    def parseFromLines(id, lines):
        state = TeamState(id)
        i = 0
        for player in TeamState.keyset:
            state[player] = PlayerState.parseFromLine(lines[i], player)
            i += 1
        return state
    
class GameState:
    def __init__(self):
        self.ball = BallState()
        self.teamA = TeamState(1)
        self.teamB = TeamState(2)

    def getLines(self):
        lines = []
        lines.append(self.ball.toLine())
        for team in [self.teamA, self.teamB]:
            lines += team.getLines()
        return lines
    
    def __str__(self):
        s =  'State:'
        s += '\n Ball:  '+str(self.ball)
        s += '\n TeamA: '+str(self.teamA)
        s += '\n TeamB: '+str(self.teamB)
        return s+'\n'

    def __repr__(self):
        return self.__str__()
    
    @staticmethod
    def parseFromLines(lines):
        state = GameState()
        state.ball = BallState.parseFromLine(lines[0])
        state.teamA = TeamState.parseFromLines(1, lines[1:6])
        state.teamB = TeamState.parseFromLines(2, lines[6:])
        return state

class PlayerMovement:
    def __init__(self):
        self.angVelocity = 0
        self.linearVelocity = 0

    def toLine(self):
        return '%x,%x'%(self.linearVelocity, self.angVelocity)
 
    def __str__(self):
        return "ang: %f, linear: %f\n"%(self.angVelocity, self.linearVelocity)

    def __repr__(self):
        return self.__str__()
   
    @staticmethod
    def parseFromLine(line):
        mov = PlayerMovement()
        parts = line.split(',')
        mov.linearVelocity = int(strhex(parts[0]))
        mov.angVelocity = int(strhex(parts[1]))
        return mov

class TeamMovement(dict):
    keyset = ['goalkeeper','player1','player2','player3','player4']

    def __init__(self, time=0, id=0):
        self.time = time
        self.id = id
        for k in TeamMovement.keyset:
            self[k] = PlayerMovement()

    def getLines(self):
        lines = []
        lines.append('%x,%x'%(self.time, self.id))
        for player in TeamMovement.keyset:
            lines.append(self[player].toLine())
        return lines
    
    def __str__(self):
        s =  'Movement:\n'
        for k in TeamMovement.keyset:
            s += str(self[k])
        return s+'\n'

    def __repr__(self):
        return self.__str__()
    
    @staticmethod
    def parseFromLines(lines):
        parts = lines[0].split(',')
        mov = TeamMovement()
        mov.time = int(strhex(parts[0]))
        mov.id = int(strhex(parts[1]))
        i = 1
        for player in TeamMovement.keyset:
            mov[player] = PlayerMovement.parseFromLine(lines[i])
            i += 1
        return mov

class GUIEvent:
    # type
    INICI = 0
    GOAL = 1
    FAULT = 2
    FIPART = 3
    FIPARTIT = 4
    CONNECT = 5
    XIULET = 6
    
    # team
    TEAM_A = 1
    TEAM_B = 2
    
    # fault
    PENALTY = 0
    FREE_KICK = 1
    FREE_BALL = 2
    
    def __init__(self):
        self.type = -1
        self.fault = -1
        self.team = -1
        self.data = ''

class Event: # Network event
    XIULET = 0
    INICI = 1
    GOL = 2
    FAULT = 3
    FIPART = 4
    FIPARTIT = 5
    
    PENALTY = 0
    FREE_KICK = 1
    FREE_BALL = 2

    TEAM_A = 1
    TEAM_B = 2
    
    def __init__(self):
        self.type = -1
        self.fault = -1
        self.team = -1
